﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class NewContact : Form
    {
        private bool _isUpdated = false;
        private Contact _editContact;

        public NewContact()
        {
            InitializeComponent();
        }

        public NewContact(Client client)
        {
            InitializeComponent();
            //this.Text = "Update Contact [" + client.ContactName + "]";
            //txtFirmName.Text = client.ClientName;
            //txtFirstName.Text = client.ContactName;
            //txtCountry.Text = client.Country;
            //txtCity.Text = "Ho Chi Minh City";
            //txtStreet.Text = "123 ABCD, E Ward, F Dist.";
            //btnCreate.Text = "Update";

            _isUpdated = true;
            _editContact = client.Contact;
            FillInformation(_editContact);
        }

        public NewContact(Contact contact)
        {
            InitializeComponent();
            //this.Text = "Update Contact [" + client.ContactName + "]";
            //txtFirmName.Text = client.ClientName;
            //txtFirstName.Text = client.ContactName;
            //txtCountry.Text = client.Country;
            //txtCity.Text = "Ho Chi Minh City";
            //txtStreet.Text = "123 ABCD, E Ward, F Dist.";
            //btnCreate.Text = "Update";

            _isUpdated = true;
            _editContact = contact;
            FillInformation(_editContact);
        }

        private void FillInformation(Contact contact)
        {
            this.Text = "Edit Contact [" + contact.ContactFirstName + ", " + contact.ContactMiddleName + " " +
                        contact.ContactLastName + "]";

            txtTitle.Text = contact.ContactPosition;
            txtFirstname.Text = contact.ContactFirstName;
            txtLastName.Text = contact.ContactLastName;
            txtMiddleName.Text = contact.ContactMiddleName;
            txtStreet.Text = contact.ContactAddress;
            txtCity.Text = contact.ContactCity;
            txtState.Text = contact.ContactState;
            txtZip.Text = contact.ContactZip;
            txtCountry.Text = contact.ContactCountry;
            txtAttention.Text = contact.ContactAttention;
            txtBusinessPhone.Text = contact.ContactBusinessPhone;
            txtHomePhone.Text = contact.ContactHomePhone;
            txtCellPhone.Text = contact.ContactCellPhone;
            txtOtherPhone.Text = contact.ContactOther;
            txtEmail1.Text = contact.ContactEmail1;
            txtEmail2.Text = contact.ContactEmail2;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Data saved!");
            if (!_isUpdated)
            {
                Common.Entity.AddToContacts(new Contact()
                                                {
                                                    ContactPosition = txtTitle.Text,
                                                    ContactFirstName = txtFirstname.Text,
                                                    ContactMiddleName = txtMiddleName.Text,
                                                    ContactLastName = txtLastName.Text,
                                                    ContactAddress = txtStreet.Text,
                                                    ContactCity = txtCity.Text,
                                                    ContactState = txtState.Text,
                                                    ContactZip = txtZip.Text,
                                                    ContactCountry = txtCountry.Text,
                                                    ContactAttention = txtAttention.Text,
                                                    ContactBusinessPhone = txtBusinessPhone.Text,
                                                    ContactCellPhone = txtCellPhone.Text,
                                                    ContactHomePhone = txtHomePhone.Text,
                                                    ContactEmail1 = txtEmail1.Text,
                                                    ContactEmail2 = txtEmail2.Text,
                                                    ContactFax = txtFax.Text,
                                                    ContactOther = txtOtherPhone.Text,
                                                    ContactID = Guid.NewGuid().ToString(),
                                                    CreatedDate = DateTime.Now,
                                                    UpdatedDate = DateTime.Now
                                                });
            }
            else
            {
                _editContact.ContactPosition = txtTitle.Text;
                _editContact.ContactFirstName = txtFirstname.Text;
                _editContact.ContactMiddleName = txtMiddleName.Text;
                _editContact.ContactLastName = txtLastName.Text;
                _editContact.ContactAddress = txtStreet.Text;
                _editContact.ContactCity = txtCity.Text;
                _editContact.ContactState = txtState.Text;
                _editContact.ContactZip = txtZip.Text;
                _editContact.ContactCountry = txtCountry.Text;
                _editContact.ContactAttention = txtAttention.Text;
                _editContact.ContactBusinessPhone = txtBusinessPhone.Text;
                _editContact.ContactCellPhone = txtCellPhone.Text;
                _editContact.ContactHomePhone = txtHomePhone.Text;
                _editContact.ContactEmail1 = txtEmail1.Text;
                _editContact.ContactEmail2 = txtEmail2.Text;
                _editContact.ContactFax = txtFax.Text;
                _editContact.ContactOther = txtOtherPhone.Text;
                _editContact.UpdatedDate = DateTime.Now;
            }
            Common.Entity.SaveChanges();
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
